// baseUrl配置测试
// 验证 larapress_app_package/common/lib/export/http.dart 中的baseUrl配置

import 'dart:io';
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('baseUrl配置测试', () {
    const httpFilePath = 'larapress_app_package/common/lib/export/http.dart';

    test('HTTP配置文件应该存在', () {
      final httpFile = File(httpFilePath);
      expect(
        httpFile.existsSync(),
        isTrue,
        reason: 'HTTP配置文件 $httpFilePath 不存在',
      );
    });

    test('应该包含baseUrl配置', () {
      final httpFile = File(httpFilePath);
      expect(httpFile.existsSync(), isTrue);

      final content = httpFile.readAsStringSync();
      expect(content.contains('baseUrl:'), isTrue, reason: '未找到baseUrl配置');
    });

    test('baseUrl应该是有效的URL格式', () {
      final httpFile = File(httpFilePath);
      expect(httpFile.existsSync(), isTrue);

      final content = httpFile.readAsStringSync();

      // 提取baseUrl
      final baseUrlMatch = RegExp(r"baseUrl:\s*'([^']*)'").firstMatch(content);
      expect(baseUrlMatch, isNotNull, reason: '无法解析baseUrl配置');

      final baseUrl = baseUrlMatch!.group(1)!;
      expect(baseUrl.isNotEmpty, isTrue, reason: 'baseUrl不能为空');

      // 验证URL格式
      expect(
        baseUrl.startsWith('http://') || baseUrl.startsWith('https://'),
        isTrue,
        reason: 'baseUrl必须以 http:// 或 https:// 开头',
      );

      // 验证URL解析
      expect(
        () => Uri.parse(baseUrl),
        returnsNormally,
        reason: 'baseUrl不是有效的URL格式',
      );

      final uri = Uri.parse(baseUrl);
      expect(uri.host.isNotEmpty, isTrue, reason: 'URL必须包含有效的主机名');
    });

    test('生产环境应该使用HTTPS', () {
      final httpFile = File(httpFilePath);
      expect(httpFile.existsSync(), isTrue);

      final content = httpFile.readAsStringSync();
      final baseUrlMatch = RegExp(r"baseUrl:\s*'([^']*)'").firstMatch(content);
      final baseUrl = baseUrlMatch!.group(1)!;

      // 如果不是本地开发环境，应该使用HTTPS
      if (!baseUrl.contains('localhost') && !baseUrl.contains('127.0.0.1')) {
        expect(
          baseUrl.startsWith('https://'),
          isTrue,
          reason: '生产环境应该使用HTTPS协议',
        );
      }
    });

    test('URL应该不包含敏感信息', () {
      final httpFile = File(httpFilePath);
      expect(httpFile.existsSync(), isTrue);

      final content = httpFile.readAsStringSync();
      final baseUrlMatch = RegExp(r"baseUrl:\s*'([^']*)'").firstMatch(content);
      final baseUrl = baseUrlMatch!.group(1)!;

      // 检查URL中不应包含敏感信息
      final sensitivePatterns = ['password', 'token', 'key', 'secret', 'admin'];

      for (final pattern in sensitivePatterns) {
        expect(
          baseUrl.toLowerCase().contains(pattern),
          isFalse,
          reason: 'baseUrl不应包含敏感信息: $pattern',
        );
      }
    });

    test('HTTP配置应该包含必要的超时设置', () {
      final httpFile = File(httpFilePath);
      expect(httpFile.existsSync(), isTrue);

      final content = httpFile.readAsStringSync();

      // 检查超时配置
      expect(content.contains('connectTimeout'), isTrue, reason: '缺少连接超时配置');
      expect(content.contains('receiveTimeout'), isTrue, reason: '缺少接收超时配置');
      expect(content.contains('sendTimeout'), isTrue, reason: '缺少发送超时配置');
    });

    test('HTTP配置应该包含正确的默认头部', () {
      final httpFile = File(httpFilePath);
      expect(httpFile.existsSync(), isTrue);

      final content = httpFile.readAsStringSync();

      // 检查默认头部
      expect(
        content.contains('Content-Type'),
        isTrue,
        reason: '缺少Content-Type头部',
      );
      expect(content.contains('Accept'), isTrue, reason: '缺少Accept头部');
      expect(
        content.contains('application/json'),
        isTrue,
        reason: '应该使用JSON格式',
      );
    });
  });

  group('URL详细验证', () {
    late String baseUrl;

    setUp(() {
      final httpFile = File(
        'larapress_app_package/common/lib/export/http.dart',
      );
      final content = httpFile.readAsStringSync();
      final baseUrlMatch = RegExp(r"baseUrl:\s*'([^']*)'").firstMatch(content);
      baseUrl = baseUrlMatch!.group(1)!;
    });

    test('URL组件详细验证', () {
      final uri = Uri.parse(baseUrl);

      // 检查协议
      expect(
        ['http', 'https'].contains(uri.scheme),
        isTrue,
        reason: '只支持HTTP和HTTPS协议',
      );

      // 检查主机名
      expect(uri.host.isNotEmpty, isTrue, reason: '主机名不能为空');
      expect(uri.host.contains(' '), isFalse, reason: '主机名不能包含空格');

      // 检查端口（如果有）
      if (uri.hasPort) {
        expect(
          uri.port > 0 && uri.port <= 65535,
          isTrue,
          reason: '端口号必须在1-65535范围内',
        );
      }
    });

    test('URL安全性检查', () {
      // 检查是否使用安全协议
      if (!baseUrl.contains('localhost') && !baseUrl.contains('127.0.0.1')) {
        expect(baseUrl.startsWith('https://'), isTrue, reason: '生产环境必须使用HTTPS');
      }

      // 检查路径是否合理
      final uri = Uri.parse(baseUrl);
      if (uri.path.isNotEmpty && uri.path != '/') {
        expect(uri.path.startsWith('/'), isTrue, reason: 'API路径应该以/开头');
      }
    });
  });
}
